home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / plotting / pcgplots / gptfobj.cpp < prev    next >
C/C++ Source or Header  |  1992-04-24  |  10KB  |  304 lines

  1. /*-----------------------------------------------
  2.     GptFObj.CPP -- File Open, Save Dialog Boxes
  3.     Class FileObject
  4.   -----------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "gpt.h"
  8. #include <stdio.h>
  9. //#include <stdarg.h>
  10.  
  11. FileObjectPt FileObject::ThisFileObject=NULL;
  12.  
  13.  
  14. BOOL UpdateListBox(HWND, cStr FilePath, cStr FileSpec);
  15.  
  16.  
  17.  
  18. LPSTR lstrchr  (LPSTR str, char ch) ;
  19. LPSTR lstrrchr (LPSTR str, char ch) ;
  20.  
  21. static char      szDefExt   [5]  ;
  22. static char      szFileName [96] ;
  23. static char      szFileSpec [16] ;
  24. static WORD      wFileAttr, wStatus ;
  25.  
  26.  
  27. BOOL FAR PASCAL _export FileObject::FileNewDlgProc(HWND hDlg, WORD message,
  28.                                                                 WORD wParam, LONG lParam)
  29.       {
  30.       char  cLastChar ;
  31.       short nEditLen ;
  32.       char stg[255];
  33.       switch (message)  // handle WM_INITDIALOG, WMCOMMAND
  34.           {
  35.           case WM_INITDIALOG:
  36.           ThisFileObject= (FileObjectPt)GetObjectPt(lParam);
  37.           if (!UpdateListBox(hDlg, ThisFileObject->Path,
  38.                                             ThisFileObject->Spec))  MessageBeep (0) ;
  39.           SetDlgItemText(hDlg,IDD_MESS, ThisFileObject->DialogMess);
  40.           return TRUE ;
  41.  
  42.           case WM_COMMAND:
  43.           switch (wParam)   // control ID's IDD_FLIST, IDD_FNAME, IDOK, IDCANCEL
  44.               {
  45.               case IDD_FLIST:        // Something done with listbox
  46.               switch (HIWORD (lParam))   // Handles LBN_SELCHANGE, LBN_DBLCLK
  47.                   {
  48.                   case LBN_DBLCLK:
  49.                   if ( FileObject::GetNewChoice(hDlg, CREATEFILE) )   // file selected and opened
  50.                       EndDialog (hDlg, FILE_SELECTED);
  51.                   return TRUE;
  52.  
  53.                   case LBN_SELCHANGE:   // listbox selection change
  54.                   if (DlgDirSelect (hDlg, stg, IDD_FLIST))  // a path selected
  55.                         strcat(stg, ThisFileObject->Spec);              // append filename
  56.                   SetDlgItemText (hDlg, IDD_FNAME, stg) ;
  57.                   return TRUE ;
  58.                   }   // End  switch (HIWORD (lParam))
  59.               break ;     // End case IDD_FLIST
  60.  
  61.               case IDD_FNAME:
  62.               if (HIWORD (lParam) == EN_CHANGE) EnableWindow (
  63.                  GetDlgItem (hDlg, IDOK), (BOOL) SendMessage (LOWORD (lParam),
  64.                                                             WM_GETTEXTLENGTH, 0, 0L)) ;
  65.               return TRUE ;
  66.  
  67.               case IDOK:
  68.               if ( FileObject::GetNewChoice(hDlg, CREATEFILE))   // file selected and opened
  69.                       EndDialog (hDlg, FILE_SELECTED);
  70.               return TRUE;
  71.               case IDCANCEL: EndDialog (hDlg, NO_FILE_SELECTED);  return TRUE ;
  72.               }   // End switch (wParam)
  73.           break; // End case WM_COMMAND
  74.           }      // end switch (message)
  75.       return FALSE ;
  76.       }
  77.  
  78. BOOL FAR PASCAL _export FileObject::FileOpenDlgProc (HWND hDlg, WORD message,
  79.                                                                 WORD wParam, LONG lParam)
  80.       {
  81.       char  cLastChar ;
  82.       short nEditLen ;
  83.       char stg[255];
  84.       switch (message)  // handle WM_INITDIALOG, WMCOMMAND
  85.           {
  86.           case WM_INITDIALOG:
  87.           ThisFileObject= (FileObjectPt)GetObjectPt(lParam);
  88.           if (!UpdateListBox(hDlg, ThisFileObject->Path,
  89.                                             ThisFileObject->Spec))  MessageBeep (0) ;
  90.           SetDlgItemText(hDlg,IDD_MESS, ThisFileObject->DialogMess);
  91.           return TRUE ;
  92.  
  93.           case WM_COMMAND:
  94.           switch (wParam)   // control ID's IDD_FLIST, IDD_FNAME, IDOK, IDCANCEL
  95.               {
  96.               case IDD_FLIST:        // Something done with listbox
  97.               switch (HIWORD (lParam))   // Handles LBN_SELCHANGE, LBN_DBLCLK
  98.                   {
  99.                   case LBN_DBLCLK:
  100.                   if ( FileObject::GetNewChoice(hDlg, READFILE) )   // file selected and opened
  101.                       EndDialog (hDlg, FILE_SELECTED);
  102.                   return TRUE;
  103.  
  104.                   case LBN_SELCHANGE:   // listbox selection change
  105.                   if (DlgDirSelect (hDlg, stg, IDD_FLIST))  // a path selected
  106.                         strcat(stg, ThisFileObject->Spec);              // append filename
  107.                   SetDlgItemText (hDlg, IDD_FNAME, stg) ;
  108.                   return TRUE ;
  109.                   }   // End  switch (HIWORD (lParam))
  110.               break ;     // End case IDD_FLIST
  111.  
  112.               case IDD_FNAME:
  113.               if (HIWORD (lParam) == EN_CHANGE) EnableWindow (
  114.                  GetDlgItem (hDlg, IDOK), (BOOL) SendMessage (LOWORD (lParam),
  115.                                                             WM_GETTEXTLENGTH, 0, 0L)) ;
  116.               return TRUE ;
  117.  
  118.               case IDOK:
  119.               if ( FileObject::GetNewChoice(hDlg,READFILE))   // file selected and opened
  120.                       EndDialog (hDlg, FILE_SELECTED);
  121.               return TRUE;
  122.               case IDCANCEL: EndDialog (hDlg, NO_FILE_SELECTED);  return TRUE ;
  123.               }   // End switch (wParam)
  124.           break; // End case WM_COMMAND
  125.           }      // end switch (message)
  126.       return FALSE ;
  127.       }
  128.  
  129. //  FileObject::DialogSetup - Setup Object variables for use in a dialog
  130. void   FileObject::DialogFileSetup(cStr Mess, cStr Path, cStr Spec,    cStr Ext, WORD wAttr)
  131.     {
  132.     _fstrcpy( this->Path, Path);
  133.     _fstrcpy(this->Spec, Spec);
  134.     _fstrcpy(this->Ext, Ext);
  135.     this->DialogMess = new char[_fstrlen(Mess) + 1];
  136.     _fstrcpy(this->DialogMess, Mess);
  137.     this->HFile = 0;
  138.     this->wAttr = wAttr;
  139.     }
  140.  
  141.  
  142. //  FileObject::DialogFileInfo - Move static Setup Object variables to local
  143. //                               FileObject
  144. void   FileObject::DialogFileInfo()
  145.     {
  146.     //strcpy(LocalPath, StaticPath);
  147.     //strcpy(LocalSpec, StaticSpec);
  148.     //strcpy(LocalExt,  StaticExt);
  149.     //LocalHFile = StaticHFile;
  150.     }
  151.  
  152. //  FileObject::OpenRead - return TRUE if file opened for read
  153. BOOL FileObject::OpenRead(  HWND hWndParent  )
  154.     {
  155.     DWORD lParam;
  156.     FARPROC lpfnDlgProc = MakeProcInstance(
  157.           (FARPROC)FileObject::FileOpenDlgProc, Appl::hInstance) ;
  158.     // Do dialog passing pointer to file object
  159.     SetObjectPt(this, &lParam);
  160.     int iReturn = DialogBoxParam ( Appl::hInstance, "FileOpen",  hWndParent,
  161.                           lpfnDlgProc,lParam) ;
  162.     FreeProcInstance(lpfnDlgProc);
  163.     if (iReturn == FILE_SELECTED )
  164.         {
  165.         /* MessageBox(hWndParent, "New File Selected", "Dialog Result",
  166.                                   MB_ICONINFORMATION | MB_OK); */
  167.         return TRUE;
  168.         }
  169.     else return FALSE;   // no file opened
  170.     }
  171. //  FileObject::OpenWrite - return TRUE if file opened for write
  172. BOOL FileObject::OpenWrite(  HWND hWndParent  )
  173.     {
  174.     DWORD lParam;
  175.     FARPROC lpfnFileDlgProc = MakeProcInstance(
  176.           (FARPROC)FileObject::FileNewDlgProc, Appl::hInstance) ;
  177.     // Do dialog passing pointer to file object
  178.     SetObjectPt(this, &lParam);
  179.     int iReturn = DialogBoxParam ( Appl::hInstance, "FileOpen",  hWndParent,
  180.                           lpfnFileDlgProc,lParam) ;
  181.     FreeProcInstance(lpfnFileDlgProc);
  182.     if (iReturn == FILE_SELECTED )
  183.         {
  184.         /* MessageBox(hWndParent, "New File Selected", "Dialog Result",
  185.                                   MB_ICONINFORMATION | MB_OK); */
  186.         return TRUE;
  187.         }
  188.     else return FALSE;   // no file opened
  189.     }
  190.  
  191. // **** UpdateListBox - use local path, filespec to update
  192. //                      listbox, edit controls
  193. BOOL UpdateListBox(HWND hDlg, cStr Path, cStr Spec)
  194.     {
  195.     char stg[255];
  196.     BOOL NotWild;
  197.     _fstrcpy(stg, Path);
  198.     _fstrcat(stg, Spec);
  199.     NotWild = FALSE;
  200.     if (!lstrchr((LPSTR) stg,'*')  && !lstrchr  ((LPSTR) stg,'?') )
  201.         {
  202.         strcat(stg,"*");
  203.         NotWild = TRUE;
  204.         }
  205.     if ( DlgDirList(hDlg, stg, IDD_FLIST,IDD_FPATH, 0X4010))
  206.         {
  207.         SendDlgItemMessage (hDlg, IDD_FNAME, EM_LIMITTEXT, 80, 0L) ;
  208.         if (NotWild) stg[strlen(stg) - 1] = 0;
  209.         SetDlgItemText(hDlg, IDD_FNAME, stg);
  210.         SendDlgItemMessage(hDlg,IDD_FLIST,LB_SELECTSTRING,-1,
  211.                                                                   (long)(LPSTR)stg);
  212.         return TRUE;
  213.         }
  214.     return FALSE;
  215.     }
  216.  
  217. // ******* GetNewChoice()
  218. // User has selected new directory or file
  219. // Return True if valid file selected
  220. BOOL FileObject::GetNewChoice(HWND hDlg, enum Modes FileAccess )
  221.     {
  222.     LPOFSTRUCT pof = &(ThisFileObject->OF);
  223.     char EditName[255];
  224.     int iret;
  225.     iret = GetDlgItemText(hDlg, IDD_FNAME, EditName, 128);
  226.     int nEditLen  = lstrlen ( EditName) ;
  227.     char cLastChar = *AnsiPrev (EditName, EditName + nEditLen) ;
  228.  
  229.     if (cLastChar == '\\' || cLastChar == ':')
  230.                                           lstrcat (EditName, ThisFileObject->Spec) ;
  231.     if (strchr(EditName,'*') || strchr(EditName,'?'))  // wildcardname
  232.         {
  233.         SeparateFile( (LPSTR)ThisFileObject->Path, (LPSTR)ThisFileObject->Spec,
  234.                  (LPSTR)ThisFileObject->Ext, (LPSTR)EditName);
  235.         if (!UpdateListBox(hDlg,ThisFileObject->Path,ThisFileObject->Spec))
  236.                                                                   MessageBeep(1);
  237.         return FALSE;
  238.         }
  239.     // no wildcard  - a directory name ??
  240.  
  241.     EditName[iret] = '\\';   // make into directory name
  242.     EditName[iret+1] = 0;
  243.     // See if a directory
  244.     if (UpdateListBox(hDlg, EditName,ThisFileObject->Spec)) return FALSE; // yes
  245.     // assume a file and try to open
  246.     EditName[iret] = 0;
  247.     if ( FileAccess == READFILE)      // opening for read
  248.         {
  249.         iret = OpenFile (EditName, pof, OF_READ | OF_SHARE_DENY_WRITE );
  250.         if (-1 != iret)
  251.             {
  252.  
  253.             // opened file
  254.             ThisFileObject->HFile = iret;
  255.             GetDlgItemText(hDlg, IDD_FPATH, ThisFileObject->Path, 128);
  256.             strcat(ThisFileObject->Path,"\\");
  257.             strcpy(ThisFileObject->Spec, EditName);
  258.             return TRUE;
  259.             }
  260.         // add extension in none already there
  261.         if (!lstrchr ((LPSTR) EditName, '.') )
  262.             {
  263.             // no try to add and see if file exists
  264.             lstrcat (EditName,ThisFileObject->Ext);
  265.             if (-1 != OpenFile (EditName, pof, OF_EXIST )) // exists
  266.                 {
  267.                 SetDlgItemText(hDlg, IDD_FNAME, EditName);
  268.                 return FALSE; // make user verify
  269.                 }
  270.             }
  271.         MessageBeep(0) ;
  272.         OKMsgBox( "Try Again", "** %s **", sys_errlist[pof->nErrCode]);
  273.         return FALSE;
  274.         }
  275.  
  276.     if ( FileAccess == CREATEFILE) // try opening for create/overwrite
  277.         {
  278.         iret = OpenFile (EditName, pof, OF_EXIST ); // See if would overwite
  279.         if (-1 != iret)    // file exists
  280.             {
  281.             if (!OKCancelMsgBox( "Overwrite??"," %s Exists - Overwrite?",
  282.                                           EditName)) return FALSE;
  283.           //    iret = OpenFile(EditName, pof, OF_DELETE );
  284.             iret = OpenFile(EditName, pof, OF_CREATE | OF_SHARE_EXCLUSIVE );
  285.             if (-1 != iret)  { ThisFileObject->HFile = iret; return TRUE;  }
  286.             else
  287.                 {
  288.                 OKMsgBox( "Open Failure","Could not Open %s for Write",EditName);
  289.                 return FALSE;
  290.                 }
  291.             }
  292.         else    // file does not exist - try to open
  293.             {
  294.             iret = OpenFile(EditName, pof, OF_CREATE | OF_SHARE_EXCLUSIVE );
  295.             if (-1 != iret)  { ThisFileObject->HFile = iret; return TRUE;  }
  296.             }
  297.         MessageBeep(0) ;
  298.         OKMsgBox( "Try Again", "** %s **", sys_errlist[pof->nErrCode]);
  299.         return FALSE;
  300.         }
  301.     return FALSE;
  302.     }
  303.  
  304.